float4x4    WorldViewProj : WORLDVIEWPROJECTION;
float3      Ambient;
float4      MatDiffuse;

float3      LightDiffuse1;
float3      LightDir1;

float3      LightDiffuse2;
float3      LightDir2;

struct VS_INPUT
{
    float3  Pos     : POSITION;
    float3  Normal  : NORMAL;
    float2  Tex0    : TEXCOORD0;
};

struct VS_OUTPUT
{
    float4  Pos     : POSITION;
    float4  Color   : COLOR;
    float2  Tex0    : TEXCOORD0;
};

VS_OUTPUT VShade(VS_INPUT In, uniform float3 NormalFactor)
{
    VS_OUTPUT Out = (VS_OUTPUT) 0; 

    Out.Pos       = mul(WorldViewProj, float4(In.Pos, 1));
    Out.Tex0      = In.Tex0;
    float3 Normal = In.Normal*NormalFactor;                      // N or -N

    // directional light 1

    float3 Color1 = max(0, dot(Normal, -LightDir1)) *MatDiffuse.rgb*LightDiffuse1;

    // directional light 2

    float3 Color2 = max(0, dot(Normal, -LightDir2)) *MatDiffuse.rgb*LightDiffuse2;

    // final color

    Out.Color.rgb = Color1+Color2+Ambient;
    Out.Color.a   = MatDiffuse.a;

    return Out;
}

////////////////////////////////////////

texture		frontTex;
sampler		frontSampler = 
sampler_state
{
    Texture			= < frontTex >;
    MipFilter		= LINEAR;
    MinFilter		= LINEAR;
    MagFilter		= LINEAR;
};

texture		backTex;
sampler		backSampler = 
sampler_state
{
    Texture			= < backTex >;
    MipFilter		= LINEAR;
    MinFilter		= LINEAR;
    MagFilter		= LINEAR;
};

struct PS_INPUT
{
    float4  Color   : COLOR0;
    float2  Tex0    : TEXCOORD0;
};

struct PS_OUTPUT
{
    float4  Color   : COLOR;
};

PS_OUTPUT PShade(PS_INPUT In, uniform sampler diffuseTex)
{
    PS_OUTPUT Out = (PS_OUTPUT) 0; 

    Out.Color = In.Color * tex2D(diffuseTex, In.Tex0);
    return Out;
}

////////////////////////////////////////

technique DoubleSidedTechnique
{
    pass P0
    {
		CullMode = CW;
				
        vertexShader = compile vs_2_0 VShade(float3(1,1,1));
        pixelShader  = compile ps_2_0 PShade(frontSampler);
    }
    
    pass P1
    {
		CullMode = CCW;
		
        vertexShader = compile vs_2_0 VShade(float3(-1,-1,-1));
        pixelShader  = compile ps_2_0 PShade(backSampler);
    }
}
